//不要放弃啊！加油啊！
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(){
    std::string server_ip = "127.0.0.1";
    int server_port = 8080;
    //创建套接字
    int socket_fd = socket(AF_INET,SOCK_STREAM,0);
    //设置服务器地址结构
    struct sockaddr_in server_addr;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);
    inet_pton(AF_INET,server_ip.c_str(),&server_addr.sin_addr);
    //请求连接
    if(connect(socket_fd,(sockaddr*)(&server_addr),sizeof(server_addr))!= 0){
        perror("connect failed");
        close(socket_fd);
        exit(EXIT_FAILURE);
    }
    int flag = 1;
    //接受和发送数据
    while(flag){
        char recv_buff[1024];
        char send_buff[1024] = "hello ,i am cilent";
        send(socket_fd,send_buff,sizeof(send_buff),0);
        //这里用于发送数据和接受数据
        recv(socket_fd,recv_buff,sizeof(recv_buff),0);
        std::cout<<"server say:"<<recv_buff<<std::endl;
    }
    //关闭套接字
    close(socket_fd);
    return  0;
}